<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = (int)$_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];


$db = new DB();
if(Check::isEmpty($_GET["belong"])) {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_lists WHERE id={$id}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res['content'] = getCont($id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {
        $sql = "SELECT * FROM qilian_lists ORDER BY id DESC";
        $res = array();
        if($res = $db->getAll($sql)) {
            foreach($res as $index=>$val) {
                $res[$index]['content'] = getCont($val['id'], $db);
            }
            exit(Result::jsonReturn(0, "查询成功", $res));
        }else {
            exit(Result::jsonReturn(1, "查询失败"));
        }    
    }    
}else if($_GET["belong"] == "self") {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_lists WHERE id={$id} AND user_id={$uid}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res['content'] = getCont($id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {

        $sql = "SELECT * FROM qilian_lists WHERE user_id={$uid} ORDER BY id DESC";
        $res = array();
        if($res = $db->getAll($sql)) {
            foreach($res as $index=>$val) {
                $res[$index]['content'] = getCont($val['id'], $db);
            }
            exit(Result::jsonReturn(0, "查询成功", $res));
        }else {
            exit(Result::jsonReturn(1, "查询失败"));
        }    
    }    
}else if($_GET["belong"] == "other") {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_lists WHERE id={$id} AND user_id!={$uid}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res['content'] = getCont($id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {
        $sql = "SELECT * FROM qilian_lists WHERE user_id!={$uid} ORDER BY id DESC";
        $res = array();
        if($res = $db->getAll($sql)) {
            foreach($res as $index=>$val) {
                $res[$index]['content'] = getCont($val['id'], $db);
            }
            exit(Result::jsonReturn(0, "查询成功", $res));
        }else {
            exit(Result::jsonReturn(1, "查询失败"));
        }    
    }    
}else if($_GET["belong"] == "order") {
    // 校验是否为空
    if(!Check::isEmpty($_GET["id"])) {
        if(Check::isNum($_GET["id"]) && (int)$_GET["id"]>0) {
            $id = (int)$_GET["id"];
            $sql = "SELECT * FROM qilian_lists WHERE id={$id}";
            $res = array();
            if($res = $db->getOne($sql)) {
                $res['content'] = getCont($id, $db);
                exit(Result::jsonReturn(0, "查询成功", $res));
            }else {
                exit(Result::jsonReturn(1, "查询失败"));
            }
        }else {
            exit(Result::jsonReturn(1, "参数格式有误"));
        }
    }else {
        $sql = "SELECT * FROM qilian_lists WHERE user_id={$uid} ORDER BY id DESC";
        $res = $db->getAll($sql);
        foreach($res as $index=>$val) {
            $res[$index]['content'] = getCont($val['id'], $db);
        }
        $sql_other = "SELECT * FROM qilian_lists WHERE user_id!={$uid} ORDER BY id DESC";
        $res_other = $db->getAll($sql_other);
        foreach($res_other as $index=>$val) {
            $res_other[$index]['content'] = getCont($val['id'], $db);
        }
        exit(Result::jsonReturn(0, "查询成功", array_merge($res, $res_other)));
    }
}else {
    exit(Result::jsonReturn(1, "参数错误"));
}

function getCont($id, $db) {
    $sql = "SELECT content_id FROM qilian_relationships WHERE list_id={$id}";
    $res = $db->getAll($sql);
    $ret = array();
    foreach($res as $val) {
        array_push($ret, $val['content_id']);
    }
    return $ret;
}

?>